package com.beadhouse.outregistration.support.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beadhouse.common.dto.QueryResult;
import com.beadhouse.common.exeption.BeadHouseException;
import com.beadhouse.entity.OutEntity;
import com.beadhouse.outregistration.ao.OutAo;
import com.beadhouse.outregistration.dao.OutGoingDao;
import com.beadhouse.outregistration.service.OutGoingService;
import com.beadhouse.outregistration.util.StringUtil;
import com.fasterxml.jackson.databind.deser.std.TokenBufferDeserializer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.NonNull;

/**
 * @author admin 周彬 ..
 */
@Service
public class OutGoingServiceImpl implements OutGoingService{
	
	@Autowired
	OutGoingDao dao;
	
	@Override
	@Transactional
	public void addRegistration(@NonNull OutAo ao) throws Exception {
		//验证参数
		
		String outgoingReadson=ao.getOutgoingReason();
		String escorted=ao.getEscorted();
		String escortedTel=ao.getEscortedTel();
		String relation=ao.getRelation();
		String customerName=ao.getCustomerName();
		Integer customerId=Integer.parseInt(ao.getCustomerId());
		String remark=ao.getRemark();
		
		if("null".equals(outgoingReadson)) {
			throw new BeadHouseException("外出事由不能为空！");
		}
		if("null".equals(escorted)) {
			throw new BeadHouseException("陪同人不能为空！");
		}
		if("null".equals(escortedTel)) {
			throw new BeadHouseException("陪同人电话不能为空！");
		}
		if("null".equals(relation)) {
			throw new BeadHouseException("陪同人关系不能为空！");
		}
		if("null".equals(customerName)) {
			throw new BeadHouseException("请选择用户！");
		}
		
		
		OutEntity oe=new OutEntity();
		oe.setCreateTime(new Date());
		oe.setCreateBy(StringUtil.parseInt(ao.getCreateBy(), 123456));//currentId
		oe.setUpdateTime(StringUtil.toDate(ao.getUpdateTime(), new Date()));
		oe.setUpdateBy(StringUtil.parseInt(ao.getUpdateBy(), 1));//currentId
		oe.setNoDeleted(false);
		oe.setOutgoingReason(outgoingReadson);
		oe.setOutgoingTime(StringUtil.toDate(ao.getOutgoingTime(), new Date()));
		oe.setExpectedReturnTime(StringUtil.toDate(ao.getExpectedReturnTime(), new Date()));
		oe.setEscorted(escorted);
		oe.setEscortedTel(escortedTel);
		oe.setRelation(relation);
		oe.setAuditStatus("未审核");
		oe.setCustomerName(customerName);
		oe.setCustomerId(customerId);
		oe.setRemark(remark);
		//执行业务逻辑
		dao.insert(oe);

	}

	@Override
	@Transactional
	public void approvalOG(String id,String approval) throws Exception {
		//验证参数
		Date approTime=new Date();
		Integer newId=Integer.parseInt(id);
		try {
			if(newId!=null) {
				//执行业务逻辑
				dao.approvalOG(newId, approTime, approval);
			}
		} catch (BeadHouseException e) {
			throw new BeadHouseException("id为空！");
		}
	}

	@Override
	@Transactional
	public void deleteOG(@NotNull String id1) throws Exception {
		//验证参数
		Integer id=Integer.parseInt(id1);
		try {
			if(id!=null) {
				//执行业务逻辑
				dao.delete(id);
			}
		} catch (BeadHouseException e) {
			throw new BeadHouseException("id为空！");
		}
		
		
	}

	@Override
	@Transactional()
	public void updateOG(@NonNull OutAo ao,String currentId) throws Exception {
		//验证参数
		String outgoingReadson=ao.getOutgoingReason();
		String escorted=ao.getEscorted();
		String escortedTel=ao.getEscortedTel();
		String relation=ao.getRelation();
		Integer id=Integer.parseInt(ao.getId());
		String remark=ao.getRemark();
		try {
			if(id != null){
				OutEntity oe=new OutEntity();
				oe.setId(id);
				oe.setUpdateTime(StringUtil.toDate(ao.getUpdateTime(), new Date()));
				oe.setUpdateBy(StringUtil.parseInt(currentId, 0));
				oe.setOutgoingReason(outgoingReadson);
				oe.setOutgoingTime(StringUtil.toDate(ao.getOutgoingTime(), new Date()));
				oe.setExpectedReturnTime(StringUtil.toDate(ao.getExpectedReturnTime(), new Date()));
				oe.setActualReturnTime(StringUtil.toDate(ao.getActualReturnTime(), new Date()));
				oe.setEscorted(escorted);
				oe.setRelation(relation);
				oe.setEscortedTel(escortedTel);
				oe.setRemark(remark);
				//执行业务逻辑
				dao.updateOG(oe, currentId);
			}
		} catch (BeadHouseException e) {
			throw new BeadHouseException("id为空!");
		}
		
	}

	@Override
	@Transactional
	public QueryResult<OutEntity> selectlistOG(String key,String pageNo1,String PageSize1) throws Exception {
		//验证参数
		int pageNo=StringUtil.parseInt(pageNo1, 1);
		int pageSize=StringUtil.parseInt(PageSize1, 4);
		//执行业务逻辑
		if(key!=null) {
			key="%"+key+"%";
		}
		List<OutEntity> rows=new ArrayList<>();
		System.out.println(pageNo);
		PageHelper.startPage(pageNo, pageSize);
		rows=dao.selectListOG(key);
		
		//组装业务结果
		PageInfo<OutEntity> pageInfo = new PageInfo<>(rows);
		long totalRows=pageInfo.getTotal();
		QueryResult<OutEntity> qr=new QueryResult<>();
		qr.setPageNo(pageNo);
		qr.setPageSize(pageSize);
		qr.setTotalRows((int)totalRows);
		qr.setRows(rows);
		return qr;
	}

	@Override
	@Transactional
	public OutEntity selectByIdOG(@NotNull String id1) throws Exception {
		//验证参数
		Integer id=Integer.parseInt(id1);
		OutEntity oe=new OutEntity();
		try {
			if(id!=null) {
				//执行业务逻辑
				oe=dao.selectByIdOG(id);
				//组装业务结果
				return oe;
			}
		} catch (BeadHouseException e) {
			throw new BeadHouseException("id为空！");
		}
		return null;
	}

	@Override
	@Transactional
	public void updateTimeOG(String actualReturnTime1,String customerId1) throws Exception {
		//验证参数
		Integer customerId=Integer.parseInt(customerId1);
		Date actualReturnTime=StringUtil.toDate(actualReturnTime1, new Date());
		try {
			if(customerId!=null) {
				//执行业务逻辑
				dao.updateTimeOG(actualReturnTime,customerId);
			}
		} catch (BeadHouseException e) {
			throw new BeadHouseException("customerId为空！");
		}
	}

	
}
